<script type="text/javascript">
    (function ($) {
        var formId = '#{{$form_id}}';
        var validate_config = {};
        validate_config.message = '表单验证未通过';
        validate_config.feedbackIcons = {
            valid: 'fa fa-check',
            invalid: 'fa fa-times',
            validating: 'fa fa-refresh'
        };
        var validate_config_fields = '{{$validate_config|default=\'{}\'}}';
        validate_config.fields = eval('(' + validate_config_fields + ')');
        $(document).ready(function () {
            $(formId).bootstrapValidator(validate_config).on('success.form.bv', function (e) {
                e.preventDefault();
                if (typeof CKEDITOR !== 'undefined') {
                    for (var instance in CKEDITOR.instances) {
                        CKEDITOR.instances[instance].updateElement();
                    }
                }
                var form = $(e.target);
                var formAction = form.attr('action');
                var formData = form.serialize();
                if (formAction === '' || formAction === null) {
                    $(formId).bootstrapValidator('disableSubmitButtons', false);
                    return false;
                }
                $.ajax({
                    url: formAction,
                    type: '{{$method}}',
                    data: formData,
                    dataType: 'json',
                    success: function (re) {
                        if (re.code === 1) {
                            $(formId)[0].reset();
                            $(formId).bootstrapValidator('resetForm');
                            alertNotify(re.msg, 'success');
                            var redirectUrl = '{{$redirect_url}}';
                            redirectUrl = (redirectUrl === '') ? re.url : redirectUrl;
                            if (typeof redirectUrl !== 'undefined' && redirectUrl !== '') {
                                setTimeout(function () {
                                    window.location.href = redirectUrl;
                                }, 1500);
                            }
                        } else {
                            $(formId).bootstrapValidator('disableSubmitButtons', false);
                            alertNotify(re.msg, 'danger');
                        }
                    },
                    error: function () {
                        $(formId).bootstrapValidator('disableSubmitButtons', false);
                        alertNotify('网络错误,请重试...', 'danger');
                    }
                });
            });
        });
    }(window.jQuery));
</script>